package org.jeecg.modules.demo.dtwlwjc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.dtwlwjc.entity.ZwglDtsb;
import org.jeecg.modules.demo.dtwlwjc.mapper.ZwglDtsbMapper;
import org.jeecg.modules.demo.dtwlwjc.service.IZwglDtsbService;
import org.jeecg.modules.demo.znnqjc.service.impl.DklistServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

@Service
public class ZwglDtsbServiceImpl extends ServiceImpl<ZwglDtsbMapper, ZwglDtsb> implements IZwglDtsbService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DklistServiceImpl dklistService;
    @Autowired
    private ZwglDtsbMapper zwglDtsbMapper;

    @Override
    public String getsbwz() {
        dklistService.getToken();
        try {
            String token = (String) redisTemplate.opsForValue().get("nstoken");
//                System.out.println(token);
            String url = "http://112.102.225.89:51012/farming/openapi/getDeviceData";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("accessToken", token);
            int responseCode = con.getResponseCode();
            System.out.println("Response Code : " + responseCode);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            JSONObject jsonObject = JSON.parseObject(String.valueOf(response));

            JSONArray jsonArray = new JSONArray(jsonObject.getJSONArray("data"));
            for (int j = 0; j <= jsonArray.size(); j++) {
                JSONObject jsonObject2 = jsonArray.getJSONObject(j);
                ZwglDtsb zwglDtsb = new ZwglDtsb();
            }


        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }


        return "ok";
    }

    @Override
    public Result getqxzlb() {
        QueryWrapper<ZwglDtsb> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "气象站");
        List<ZwglDtsb> list = zwglDtsbMapper.selectList(queryWrapper);
        return Result.OK(list);
    }

    @Override
    public Result getsqzlb() {
        QueryWrapper<ZwglDtsb> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "墒情站");
        List<ZwglDtsb> list = zwglDtsbMapper.selectList(queryWrapper);
        return Result.OK(list);
    }
}
